package com.abe.mvp.view.activity;

import java.util.ArrayList;
import java.util.List;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android_serialport_api.SerialPortFinder;

import com.abe.config.SerialPortConstants;
import com.abe.config.StrengthtrainerSericalPortUtils;
import com.abe.mvp.contact.SerialPortContact;
import com.abe.mvp.model.bean.SerialPortDataBean;
import com.abe.mvp.presenter.SerialPortPresenterImp;
import com.abe.mvp.view.activity.base.BaseActivity;
import com.abe.strengthtrainer.R;
import com.abe.utils.CRCChecker;
import com.abe.utils.NormalUtils;

@ContentView(R.layout.activity_test_serialport)
public class SerialPortActivity extends BaseActivity implements
		SerialPortContact.View {
	private SerialPortContact.Presenter serialPortPresenter;

	@ViewInject(R.id.show_exercise_button_back)
	private LinearLayout show_exercise_button_back;
	@ViewInject(R.id.show_exercise_text_title)
	private TextView show_exercise_text_title;

	@ViewInject(R.id.spinner)
	private Spinner spinner;
	@ViewInject(R.id.receive_data_edit)
	private EditText receive_data_edit;
	private List<String> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		serialPortPresenter.closeSerialPort();
		serialPortPresenter = null;
	}

	private void initView() {
		serialPortPresenter = new SerialPortPresenterImp(this);
		list = new ArrayList<String>();
		SerialPortFinder finder = new SerialPortFinder();
		String[] qwe = finder.getAllDevicesPath();
		String xString = "";
		for (int i = 0; i < qwe.length; i++) {
			list.add(qwe[i]);
			xString = xString + "---" + qwe[i];
		}
		receive_data_edit.setText(xString);
	}

	@Event(value = R.id.show_exercise_button_back, type = View.OnClickListener.class)
	private void buttonBack(View view) {
		back();
	}

	@Event(value = R.id.serialport_open, type = View.OnClickListener.class)
	private void conn(View view) {
		serialPortPresenter
				.openSerialPort(SerialPortConstants.SerialPort_ttys0);

	}

	@Event(value = R.id.serialport_start, type = View.OnClickListener.class)
	private void start(View view) {

	}

	@Event(value = R.id.serialport_stop, type = View.OnClickListener.class)
	private void stop(View view) {

	}

	@Event(value = R.id.serialport_read_speed, type = View.OnClickListener.class)
	private void read_speed(View view) {

	}

	@Event(value = R.id.serialport_write_speed, type = View.OnClickListener.class)
	private void write_speed(View view) {

	}

	@Event(value = R.id.serialport_read_all, type = View.OnClickListener.class)
	private void read_all(View view) {
		byte[] read_all = CRCChecker
				.getSendBuf(StrengthtrainerSericalPortUtils.READ_DATA);
		serialPortPresenter.sendCommand(read_all);
	}

	@Override
	public void onConnectSuccess() {
		showToast(R.string.serialport_open_succ);
	}

	@Override
	public void onCloseSuccess() {
		showToast(R.string.serialport_close_succ);
	}

	@Override
	public void onSendDataSuccess() {
		showToast(R.string.serialport_send_succ);
	}

	

	@Override
	public void onError(String errorMsg) {
		showToast(errorMsg);
	}

	class MyHandler extends Handler {
		public MyHandler(Looper looper) {
			super(looper);
		}

		public void handleMessage(Message msg) {
			receive_data_edit.setText(msg.obj.toString());
		}
	}

	@Override
	public void onReceiveData(Boolean isSuccess, SerialPortDataBean data) {
		if (isSuccess) {
			new MyHandler(Looper.getMainLooper()).sendMessage(NormalUtils
					.getMessage(0x001, data.toString()));
		}
	}
}
